﻿#define  _CRT_SECURE_NO_WARNINGS 1
#include "Sort.h"
#include "Stack.h"

void Swap(int* p1, int* p2)
{
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}

void PrintArray(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
}

//三数取中，防止栈溢出，原因：如果每次都拿到最小的或最大的，时间复杂度就是O（N^2）
int GetMidIndex(int* a, int begin, int end)
{
	int mid = (begin + end) / 2;
	if (a[begin] < a[mid])
	{
		if (a[mid] < a[end])
		{
			return mid;
		}
		else if (a[begin] > a[end])
		{
			return begin;
		}
		else
			return end;
	}
	else //a[begin] >=a[mid]
	{
		if (a[mid] > a[end])
		{
			return mid;
		}
		else if (a[begin] < a[end])
		{
			return begin;
		}
		else
		{
			return end;
		}
	}
}

void InsertSort(int* a, int n)
{
	for (int i = 0; i <n-1 ; i++)
	{
		int end = i;
		int tmp = a[end + 1];
		while (end >= 0)
		{
			if (tmp < a[end])
			{
				a[end + 1] = a[end];
				--end;
			}
			else
			{
				break;
			}
		}
		a[end+1] = tmp;
	}
}

//Hoare
int PartSort1(int* a, int begin, int end)
{
	assert(a);

	if (begin >= end)
	{
		return;
	}
	int left = begin, right = end;
	int keyi = left;
	while (left < right)
	{
		while (left < right && a[right] >= a[keyi])
		{
			--right;
		}
		while (left < right && a[left] <= a[keyi])
		{
			++left;
		}
		Swap(&a[left], &a[right]);
	}
	Swap(&a[keyi], &a[left]);
	keyi = left;

	return keyi;
}

//挖坑法
int PartSort2(int* a, int begin, int end)
{
	int key = a[begin];
	int piti = begin;
	while (begin < end)
	{
		while (begin < end && a[end] >= key)
		{
			--end;
		}
		a[piti] = a[end];
		piti = end;
		while (begin < end && a[begin] <= key)
		{
			++begin;
		}
		a[piti] = a[begin];
		piti = begin;
	}
	a[piti] = key;

	return key;
}

//前后指针法
//cur位置之前小于keyi的值   思想： 当cur遇到小于keyi的值时 ++prev 和cur交换
// ++prev是否和cur相等(相等的情况下是交换同一个数，没意义)
int PartSort3(int* a, int begin, int end)
{
	int keyi = begin;
	int cur = begin + 1;
	int prev = begin;
	int mid = GetMidIndex(a, begin, end);
	Swap(&a[keyi], &a[mid]);
	while (cur <= end)
	{
		if (a[cur] < a[keyi] && ++prev != cur)
		{
			Swap(&a[cur], &a[prev]);
		}
		cur++;
	}
	Swap(&a[prev], &a[keyi]);
	keyi = prev;

	return keyi;
}

void QuickSort(int* a, int begin, int end)
{
	assert(a);

	if (begin >= end)
	{
		return;
	}
	if ((end - begin) > 10)
	{
		int keyi = PartSort3(a, begin, end);
		//[begin,keyi-1]  keyi [keyi+1,end]
		QuickSort(a, begin, keyi - 1);
		QuickSort(a, keyi + 1, end);
	}
	else
	{
		InsertSort(a + begin, end - begin + 1);
	}
	
}

void QuickSortNonR(int* a, int begin, int end)
{
	Stack st;
	StackInit(&st);
	Stackpush(&st, end);
	Stackpush(&st, begin);
	while (!StackEmpty(&st))
	{
		int left = StackTop(&st);
		Stackpop(&st);

		int right = StackTop(&st);
		Stackpop(&st);

		int keyi = PartSort3(a,left,right);
		//[left,keyi-1] keyi [keyi+1,right]

		if (keyi + 1 < right)
		{
			Stackpush(&st, right);
			Stackpush(&st, keyi+1 );
		}

		if (left < keyi - 1)
		{
			Stackpush(&st, keyi - 1);
			Stackpush(&st, left);
		}
		
	}

	StackDestory(&st);
}

//O(NlogN) 递归版本
void _MergeSort(int* a, int begin, int end, int* tmp)
{
	if (begin >= end)
		return;

	int mid = (begin + end) / 2;
	//[begin,mid]  [mid+1,end]

	_MergeSort(a, begin, mid, tmp);
	_MergeSort(a, mid + 1, end, tmp);

	//归并
	int begin1 = begin, end1 = mid;
	int begin2 = mid + 1, end2 = end;
	int i = begin1;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
		{
			tmp[i++] = a[begin1++];
		}
		else
		{
			tmp[i++] = a[begin2++];
		}
	}
	while (begin1 <= end1)
	{
		tmp[i++] = a[begin1++];
	}

	while (begin2 <= end2)
	{
		tmp[i++] = a[begin2++];
	}

	memcpy(a + begin, tmp + begin, (end - begin + 1) * sizeof(int));
	
}

void MergeSort(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		printf("malloc fail\n");
	}
	_MergeSort(a, 0, n-1, tmp);

	free(tmp);
}

//归并  非递归版本
//void MergeSortNonR(int* a, int n)
//{
//	int* tmp = (int*)malloc(sizeof(int) * n);
//	if (tmp == NULL)
//	{
//		printf("malloc fail\n");
//	}
//
//	int gap = 1;
//	while (gap < n)
//	{
//		for (int i = 0; i < n; i += 2*gap)
//		{
//			//[i,i+gap-1] [i+gap,i+2*gap-1]
//			int begin1 = i, end1 = i + gap - 1;
//			int begin2 = i + gap , end2 = i + 2 * gap - 1;
//
//			if (end1>=n)
//			{
//				end1 = n - 1;
//				//在begin1 越界的情况下 begin2 和end2 要修正成一个不存在的区间
//				begin2 = n;
//				end2 = n - 1;
//			}else if (begin2 >= n)
//			{
//				begin2 = n;
//				end2 = n - 1;
//			}else if (end2 >= n)
//			{
//				end2 = n - 1;
//			}
//
//			int j = begin1;
//			while (begin1 <= end1 && begin2 <= end2)
//			{
//				if (a[begin1] < a[begin2])
//				{
//					tmp[j++] = a[begin1++];
//				}
//				else
//				{
//					tmp[j++] = a[begin2++];
//				}
//			}
//			while (begin1 <= end1)
//			{
//				tmp[j++] = a[begin1++];
//			}
//
//			while (begin2 <= end2)
//			{
//				tmp[j++] = a[begin2++];
//			}
//
//		}
//
//		memcpy(a, tmp, sizeof(int) * n);
//
//		gap *= 2;
//	}
//	
//
//	free(tmp);
//}


void MergeSortNonR(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		printf("malloc fail\n");
	}

	int gap = 1;
	while (gap < n)
	{
		for (int i = 0; i < n; i += 2 * gap)
		{
			//[i,i+gap-1] [i+gap,i+2*gap-1]
			int begin1 = i, end1 = i + gap - 1;
			int begin2 = i + gap, end2 = i + 2 * gap - 1;
			//这种是不归并了
			if (end1 >= n || begin2 >=n)
			{
				break;
			}else if (end2 >= n)
			{
				end2 = n - 1;
			}
			int m = end2 - begin1 + 1;
			int j = begin1;
			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] < a[begin2])
				{
					tmp[j++] = a[begin1++];
				}
				else
				{
					tmp[j++] = a[begin2++];
				}
			}
			while (begin1 <= end1)
			{
				tmp[j++] = a[begin1++];
			}

			while (begin2 <= end2)
			{
				tmp[j++] = a[begin2++];
			}

			memcpy(a+i, tmp+i, sizeof(int) * m);

		}

		gap *= 2;
	}


	free(tmp);
}